# -*- coding: utf-8 -*-
# Author: Jack Wu
# Date: 6/15/2017_
import datetime
import time
import pyperclip
import os
from wox import Wox,WoxAPI
class Translate(Wox):
    def query(self, query):
        formats = ["%Y-%m-%d %H:%M:%S","%Y-%m-%d-%H-%M-%S"]

        if not query:
            return ""

        elif query == "now":
            data = []
            # 时间戳
            stamp = time.time()
            stamp = str(int(stamp))
            data.append(stamp)
            # 时间格式
            data.append(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

        elif len(query) > 10:#非时间戳
            for i in formats:
                try:
                    timestamp = str(int(time.mktime(time.strptime(query, i))))
                except Exception:
                    continue
                else:
                    if timestamp :
                        data = timestamp
                        break

        elif len(query) == 10:#时间戳，需要转化为正常时间
            timeArray = time.localtime(float(query))
            data = str(time.strftime("%Y-%m-%d %H:%M:%S", timeArray))

        if(isinstance(data, str)):
            result = [{
                "Title": data,
                "SubTitle": '时间戳转换-按下回车复制到粘贴版 ',
                "IcoPath": "/Image/app.png",
                "JsonRPCAction": {
                    "method":"copy",
                    "parameters":[data],
                    "dontHideAfterAction":False}
            }]
        else:
            result = []
            for dat in data:
                result.append({
                    "Title": dat,
                    "SubTitle": '时间戳转换-按下回车复制到粘贴版 ',
                    "IcoPath": "/Image/app.png",
                    "JsonRPCAction": {
                        "method": "copy",
                        "parameters": [dat],
                        "dontHideAfterAction": False}
                })

        return result

    def copy(self,data):
        # command = 'set /p="' + data + '"<nul echo ' + data + '| clip'
        # os.system(command)
        pyperclip.copy(data)

if __name__ == "__main__":
    Translate()